//
//  FirstPageSearchPage_Ht.m
//  WZLProduct
//
//  Created by dllo on 16/3/24.
//  Copyright © 2016年 HaiTeng. All rights reserved.
//

#import "FirstPageSearchPage_Ht.h"
#import "FirstPageSearchPage_Ht_Cell.h"
#import "FirstPageSearchPageHeaderView_Ht.h"
#import "WHTFirstPageSearchModel.h"
@interface FirstPageSearchPage_Ht ()<UICollectionViewDataSource,UICollectionViewDelegate>
@property (nonatomic, strong) NSMutableArray *countriesOutArray; /**< 国外栏数据 */
@property (nonatomic, strong) NSMutableArray *countriesInArray; /**< 国内栏数据 */


@end



@implementation FirstPageSearchPage_Ht
NSString *FirstPageSearchPage_Ht_Cell_id = @"FirstPageSearchPage_Ht_Cell_id";
NSString *FirstPageSearchPageHeaderIdentifier = @"FirstPageSearchPageHeaderIdentifier";

- (instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        self.backgroundColor = [UIColor colorWithRed:0.91 green:0.87 blue:0.78 alpha:1];
        self.alpha = 0.8;
        
        [self initCollectionView];
        [self handelData];
    }
    return self;
}

- (void)initCollectionView{
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    layout.itemSize = CGSizeMake((kScreenWidth - 60) / 3, 60);
    layout.sectionInset = UIEdgeInsetsMake(15, 15, 0, 15);
    self.collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height) collectionViewLayout:layout];
    self.collectionView.backgroundColor = [UIColor clearColor];
    [self addSubview:self.collectionView];
    self.collectionView.delegate = self;
    self.collectionView.dataSource = self;
    [self.collectionView registerClass:[FirstPageSearchPage_Ht_Cell class] forCellWithReuseIdentifier:FirstPageSearchPage_Ht_Cell_id];
    [self.collectionView registerClass:[FirstPageSearchPageHeaderView_Ht class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:FirstPageSearchPageHeaderIdentifier];
}
#pragma mark CollectionView Delegate

/** Header大小 */
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section
{
    return CGSizeMake(kScreenWidth, 50);
}
/** 头视图 */
- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath
{
    if (kind == UICollectionElementKindSectionHeader) {
        FirstPageSearchPageHeaderView_Ht *header = [collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:FirstPageSearchPageHeaderIdentifier forIndexPath:indexPath];
 
        if (indexPath.section == 0) {
            header.sectionTitle.text = @"国外热门目的地";
 
            
        }else{
            header.sectionTitle.text = @"国内热门目的地";
 
        }
        
        return header;
    }
    return nil;
}


/**< cell将要出现,给个动画效果进行过度 */
- (void)collectionView:(UICollectionView *)collectionView willDisplayCell:(FirstPageSearchPage_Ht_Cell *)cell forItemAtIndexPath:(NSIndexPath *)indexPath{
    cell.layer.transform = CATransform3DMakeRotation(2, 0, 1, 0);
    [UIView animateWithDuration:0.5 animations:^{
        cell.layer.transform = CATransform3DMakeRotation(0, 0, 0, 0);
    }];
}
/** item个数 */
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    
    if (section == 0) {
        return self.countriesOutArray.count;
    }
    return self.countriesInArray.count;
}

/** 分组数 */
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
    return 2;
}

/** cellForItem */
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    FirstPageSearchPage_Ht_Cell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:FirstPageSearchPage_Ht_Cell_id forIndexPath:indexPath];

    if (indexPath.section == 0 && self.countriesOutArray.count) {
        cell.model = self.countriesOutArray[indexPath.row];
    }
    if (indexPath.section == 1 && self.countriesInArray.count) {
        cell.model = self.countriesInArray[indexPath.row];
    }

    return cell;
}
/** 点击事件 */
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    NSLog(@"%ld",indexPath.row);
//    if (_delegate_SearchPage_Ht && [_delegate_SearchPage_Ht respondsToSelector:@selector(FirstPageSearchPage_Ht_CellDidSelectedAction:Category:WHTFirstPageSearchModel:)]) {
//        [_delegate_SearchPage_Ht FirstPageSearchPage_Ht_CellDidSelectedAction:self Category:nil WHTFirstPageSearchModel:nil];
//    }
    
}

#pragma mark 处理数据
- (void)handelData{
[RequestData GETRequestWithURLString:KUrl_FistPage_WHT completionWithSuccess:^(id result) {
    NSArray *tempArray = result[@"data"][@"search_data"];
    
    self.countriesOutArray = [NSMutableArray array];//国外
    for (NSDictionary *dic in tempArray.firstObject[@"elements"]) {
        WHTFirstPageSearchModel *model = [[WHTFirstPageSearchModel alloc] init];
        [model setValuesForKeysWithDictionary:dic];
        [self.countriesOutArray  addObject:model];
    }
     self.countriesInArray = [NSMutableArray array]; //国内
    for (NSDictionary *dic in tempArray.lastObject[@"elements"]) {
        WHTFirstPageSearchModel *model = [[WHTFirstPageSearchModel alloc] init];
        [model setValuesForKeysWithDictionary:dic];
        [self.countriesInArray addObject:model];
    }
    
} WithFailure:^(id error) {
    
}];
}









#pragma mark 重新布局视图
- (void)layoutSubviews{
    [super layoutSubviews];
    [UIView animateWithDuration:0.3 animations:^{
        self.collectionView.frame = CGRectMake(0, 0, self.frame.size.width, self.frame.size.height);
    }];
}


@end
